package com.lagou.webwork.service;

import com.lagou.webwork.dao.ClassDao;
import com.lagou.webwork.factory.ClassFactory;
import com.lagou.webwork.model.Class;
import com.lagou.webwork.model.ListResult;
import com.lagou.webwork.model.Result;
import com.lagou.webwork.model.Student;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sjp
 * @date 2021/6/4 15:54
 * @description
 */
public class ClassService {
    private ClassDao _classDao;

    public ClassService() {
        _classDao = ClassFactory.getClassDao();
    }

    /**
     * 获取班级列表
     *
     * @param paramsMap
     * @param page
     * @param number
     * @return
     */
    public ListResult<Class> getClassList(List<Map<String, Object>> paramsMap, int page, int number) {
        int startPosition = (page - 1) * number;
        return _classDao.getClassList(paramsMap, startPosition, number);
    }

    /**
     * 添加班级
     *
     * @param addClass
     * @return
     */
    public Result<Class> addClass(Class addClass) {
//        if (checkSidRepeat(addStudent)) {
//            return new Result<>("200", new ArrayList<>(), 0, "学号重复，请重新输入！");
//        }

        ListResult<Class> listResult = _classDao.addClass(addClass);
        Result<Class> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (listResult.getTotalCount() > 0) {
            result.setDataList(listResult.getDataList());
            result.setTotalCount(1);
            result.setMsg("添加成功!");
        } else {
            result.setMsg("添加失败，请刷新后再试。");
        }

        return result;
    }

    /**
     * 批量删除班级信息
     *
     * @param idList
     * @return
     */
    public Result<Integer> deleteClassList(List<Integer> idList) {

        //todo:先判断当前需要删除的班级是否存在学生

        Result<Integer> result = new Result<>("200", new ArrayList<>(), 0, "");
        try {
            List<Integer> delIdList = new ArrayList<>();
            for (Integer id : idList) {
                if (!_classDao.isExistStudent(id)) {
                    delIdList.add(id);
                }
            }
            if (delIdList.size()>0){
                //将过滤好的需要删除的idList作为入参传入删除方法
                ListResult<Integer> listResult = _classDao.deleteClassList(delIdList);
                if (listResult.getTotalCount() > 0) {
                    result.setDataList(listResult.getDataList());
                    result.setTotalCount(listResult.getTotalCount());
                    if (listResult.getTotalCount() == idList.size()){
                        result.setMsg("删除成功!");
                    }else{
                        //过滤筛选
                        List<Integer> noDelList = idList.stream().filter(item -> !delIdList.contains(item)).collect(Collectors.toList());
                        result.setMsg("部分删除成功，Id为："+noDelList.toString()+"的班级，因存在学生信息，无法删除!");
                    }
                }else {
                    result.setMsg("删除失败，请检查是否存在学生信息，或刷新后再试。");
                }
            }else {
                result.setMsg("删除失败，请检查是否存在学生信息，或刷新后再试。");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    /**
     * 根据Id获取班级对象
     *
     * @param id
     * @return
     */
    public ListResult<Class> getClassModelById(int id) {
        return _classDao.getClassModelById(id);
    }

    /**
     * 更新班级信息
     *
     * @param updateClass
     * @return
     */
    public Result<Class> updateClass(Class updateClass) {
        ListResult<Class> listResult = _classDao.updateClass(updateClass);
        Result<Class> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (listResult.getTotalCount() > 0) {
            result.setDataList(listResult.getDataList());
            result.setTotalCount(1);
            result.setMsg("更新成功!");
        } else {
            result.setMsg("更新失败，请刷新后再试。");
        }

        return result;
    }
}
